﻿var status = 0;
var aaa = "#fUI/UIWindow2.img/QuestAlarm/BtQ/normal/0#";
var z = "#fUI/UIWindow/Quest/icon5/1#"; //"+z+"//美化
var kkk = "#fEffect/CharacterEff/1051296/1/0#";
var tz12 = "#fEffect/CharacterEff/1112924/0/0#"; //黄星
var tz13 = "#fEffect/CharacterEff/1112925/0/0#"; //蓝星
var tz14 = "#fEffect/CharacterEff/1112926/0/0#"; //红星

var head = "#fUI/UIWindow2.img/Quest/quest_info/summary_icon/summary#\r\n";
//"#fUI/UIWindow.img/PvP/Scroll/enabled/next2#";

function getAccountChars() {
    var chars = new Array();
    var ps = cm.getConnection().prepareStatement("SELECT * FROM characters WHERE accountid = ?");
    ps.setInt(1, cm.getPlayer().getAccountID());
    var rs = ps.executeQuery();
    while (rs.next()) {
        chars.push(rs.getInt("id"));
    }
    rs.close();
    ps.close();
    return chars;
}

function getAccountBossLog(bossid) {
    var chars = getAccountChars();
    var count = 0;
    var tmp = 0;
    for (var i = 0; i <= chars.length; i++) {
        var ps = cm.getConnection().prepareStatement("SELECT * FROM bosslog WHERE characterid = ? AND bossid = ?");
        ps.setInt(1, chars[i]);
        ps.setString(2, bossid);
        var rs = ps.executeQuery();
        if (rs.next()) {
            tmp = rs.getInt("count");
            if (tmp > 0) {
                count = count + tmp;
            }
        }
    }
    rs.close();
    ps.close();
    return count;
}

function start() {
    status = -1;
    action(1, 0, 0);
}

function action(mode, type, selection) {
    if (status == 0 && mode == 0) {
        cm.dispose();
        return;
    }
    if (mode == 1) {
        status++;
    } else {
        status--;
    }
    if (status == 0) {
        var selStr = head + "#b亲，在这里您可以抵用卷跟我换取一些东西，请选择您需要：#k\r\n\r\n";
        selStr += "#d您当前的抵用卷为：#r" + cm.getPlayer().getCSPoints(2) + " #d点\r\n(加油).#k\r\n\r\n";
        selStr += "#r#L0#" + aaa + " 300抵用换取#r#z5062000#x1( " + cm.getBossLog("抵用卷神奇") + " / 30次)#l\r\n";
        selStr += "#r#L1#" + aaa + " 10000抵用换取#r#z5050000##rx1#l\r\n";
        selStr += "#L4#" + aaa + " 1000抵用换取#r#z5062002#x1( " + cm.getBossLog("抵用卷魔方") + " / 10次)#l\r\n";
        selStr += "#L5#" + aaa + " 2000抵用卷换取#r#z2340000#( " + cm.getBossLog("抵用卷祝福") + " / 10次)#l\r\n";
        // selStr += "#L6#" + aaa + " 3000抵用换取#r#z2049325#x1( " + cm.getBossLog("高级装备强化卷") + " / 无限次)#l\r\n";
        cm.sendSimple(selStr);
    } else if (status == 1) {
        switch (selection) {
            case 0:
                if (cm.getPlayer().getCSPoints(2) >= 300 && getAccountBossLog("抵用卷神奇") <= 29) {
                    cm.gainNX(2, -300);
                    cm.gainItem(5062000, 1);
                    cm.setBossLog("抵用卷神奇");
                    cm.sendOk(head + "兑换成功。抵用卷减少300.");
                    cm.worldSpouseMessage(0x20, "『抵用卷兑换』" + " : " + "恭喜" + cm.getPlayer().getName() + ",在抵用商店处用300抵用卷兑换神气魔方1个。");
                    cm.dispose();
                } else {
                    cm.sendOk(head + "#r兑换失败:\r\n\r\n#b1). 你没有足够的抵用卷(请在市场挂机,或打怪获得)!\r\n2). 兑换次数达到上限，每天所有角色总共可兑换30次。");
                    cm.dispose();
                }
                break;
            case 1:
                if (cm.getPlayer().getCSPoints(2) >= 10000) {
                    cm.gainNX(2, -10000);
                    cm.gainItem(5050000, 1);
                    cm.sendOk(head + "兑换成功。抵用卷减少10000.");
                    cm.worldSpouseMessage(0x20, "『抵用卷兑换』" + " : " + "恭喜" + cm.getPlayer().getName() + ",在抵用商店处用10000抵用卷兑换洗能力值卷轴1个");
                    cm.dispose();
                } else {
                    cm.sendOk(head + "#r兑换失败:\r\n\r\n#b1). 你没有足够的抵用卷(请在市场挂机,或打怪获得)!");
                    cm.dispose();
                }
                break;
            case 4:
                if (cm.getPlayer().getCSPoints(2) >= 1000 && getAccountBossLog("抵用卷魔方") <= 9) {
                    cm.gainNX(2, -1000);
                    cm.gainItem(5062002, 1);
                    cm.setBossLog("抵用卷魔方");
                    cm.sendOk(head + "兑换成功。抵用卷减少1000.");
                    cm.worldSpouseMessage(0x20, "『抵用卷兑换』" + " : " + "恭喜" + cm.getPlayer().getName() + ",在抵用商店处用1000抵用卷兑换高级神奇魔方1个。");
                    cm.dispose();
                } else {
                    cm.sendOk(head + "#r兑换失败:\r\n\r\n#b1). 你没有足够的抵用卷(请在市场挂机,或打怪获得)!\r\n2). 兑换次数达到上限，每天所有角色总共可兑换10次。");
                    cm.dispose();
                }
                break;
            case 5:
                if (cm.getPlayer().getCSPoints(2) >= 2000 && getAccountBossLog("抵用卷祝福") <= 9) {
                    cm.gainNX(2, -2000);
                    cm.gainItem(2340000, 1, 1);
                    cm.setBossLog("抵用卷祝福");
                    cm.sendOk(head + "兑换成功。抵用卷减少2000.");
                    cm.worldSpouseMessage(0x20, "『抵用卷兑换』" + " : " + "恭喜" + cm.getPlayer().getName() + ",在抵用商店处用2000抵用卷兑换祝福卷轴1个。");
                    cm.dispose();
                } else {
                    cm.sendOk(head + "#r兑换失败:\r\n\r\n#b1). 你没有足够的抵用卷(请在市场挂机,或打怪获得)!\r\n2). 兑换次数达到上限，每天所有角色总共可兑换10次。");
                    cm.dispose();
                }
                break;
            case 6:
                if (cm.getPlayer().getCSPoints(2) >= 3000 && getAccountBossLog("高级装备强化卷轴") <= 8) {
                    cm.gainNX(2, -3000);
                    cm.gainItem(2049325, 1, 1);
                    cm.setBossLog("高级装备强化卷轴");
                    cm.sendOk(head + "兑换成功。抵用卷减少3000.");
                    cm.worldSpouseMessage(0x20, "『抵用卷兑换』" + " : " + "恭喜" + cm.getPlayer().getName() + ",在抵用商店处用3000抵用卷兑换高级装备强化卷个。");
                    cm.dispose();
                } else {
                    cm.sendOk(head + "#r兑换失败:\r\n\r\n#b1). 你没有足够的抵用卷(请在市场挂机,或打怪获得)!\r\n2)。");
                    cm.dispose();
                }
                break;
        }
    }
}